package com.hengshan.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hengshan.domain.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户表(User)表数据库访问层
 *
 * @author muxijun
 * @since 2023-12-07 18:34:20
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select({"<script>" +
            "select distinct u.id, u.username, u.email, u.phone, u.status, u.create_time " +
            "from user u left join user_role ur on u.id=ur.user_id " +
            "where (ur.role_id is null) or ur.user_id not in " +
            "(select p.id from user p inner join user_role q on p.id=q.user_id and q.role_id=#{roleId})" +
            "<if test='username != null and username != \"\"'> and u.username like concat('%',#{username},'%')</if>" +
            "<if test='phone != null and phone != \"\"'> and u.phone like concat('%',#{phone},'%')</if>"+
            "</script>"})
    IPage<User> selectUnallocatedList(Page<?> page, int roleId, String username, String phone);

    @Select({"<script>" +
            "select u.id, u.username, u.email, u.phone, u.status, u.create_time " +
            "from user u left join user_role ur on u.id=ur.user_id " +
            "where ur.role_id=#{roleId} " +
            "<if test='username != null and username != \"\"'> and u.username like concat('%',#{username},'%')</if>" +
            "<if test='phone != null and phone != \"\"'> and u.phone like concat('%',#{phone},'%')</if>"+
            "</script>"})
    IPage<User> selectAllocatedList(Page<?> page, int roleId, String username, String phone);
}
